SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
	
/******************************************************************************
**		File: 
**		Name: [dbo].a_Favorites_selList
**		Desc: 
**
**		Return values:
** 
**		Called by:   
**              
**		Parameters:
**		Input							Output
**     ----------							-----------
**
**		Auth: CodeSmith
**		Date: 1/8/2009 9:06:51 PM
*******************************************************************************
**		Change History
*******************************************************************************
**		Date:		Author:				Description:
**		--------		--------				-------------------------------------------
**    
*******************************************************************************/
CREATE PROCEDURE [dbo].[a_Favorites_selListByAlbumID]
	@AbId int,
	@OrderBy varchar(50),
	@OrderDirection varchar(5),
	@Page int,
	@PageSize int,
	@TotalRecords int output
AS

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

CREATE TABLE #Tempa_Favorites (
	RowNumber INT IDENTITY (1, 1) NOT NULL,
	ID int	
)

DECLARE @sql nvarchar(2000)
DECLARE @Top int

SET @Top = @Page*@PageSize
IF @PageSize > 0
   SET ROWCOUNT @Top
-- insert primary keys into temp table
SET @sql =	N'INSERT INTO #Tempa_Favorites ([ID]) SELECT '
SET @sql = @sql + ' [ID] FROM [dbo].[a_Favorites] WHERE AlbumID=' + Convert(varchar(50),@AbId) + ' ORDER BY [' + @OrderBy + N'] ' + @OrderDirection
EXEC (@sql)
SET ROWCOUNT 0

SELECT @TotalRecords = COUNT(*) FROM [a_Favorites]

SELECT
	[dbo].[a_Favorites].[ID],
	[UserID],
	[DateAdded],
	[AlbumID],
	[FavoriteType],
	[FavoriteID]
FROM
	#Tempa_Favorites AS tblTemp JOIN [dbo].[a_Favorites] ON
	tblTemp.ID = [dbo].[a_Favorites].ID 	
WHERE (@PageSize = 0) OR (@PageSize > 0 AND (@Page - 1)*@PageSize < RowNumber AND RowNumber <= @Page*@PageSize)
ORDER BY RowNumber

DROP TABLE #Tempa_Favorites


----------------------------


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
	
/******************************************************************************
**		File: 
**		Name: [dbo].a_Favorites_selList
**		Desc: 
**
**		Return values:
** 
**		Called by:   
**              
**		Parameters:
**		Input							Output
**     ----------							-----------
**
**		Auth: CodeSmith
**		Date: 1/8/2009 9:06:51 PM
*******************************************************************************
**		Change History
*******************************************************************************
**		Date:		Author:				Description:
**		--------		--------				-------------------------------------------
**    
*******************************************************************************/
CREATE PROCEDURE [dbo].[a_Favorites_selListCatBySSID]
	@SSID int,
	@OrderBy varchar(50),
	@OrderDirection varchar(5),
	@Page int,
	@PageSize int,
	@TotalRecords int output
AS
DECLARE @usrId varchar(50)
SELECT @usrId = usr.UserID
FROM a_Users usr, a_Sessions ss
WHERE ss.SessionID=@ssid AND ss.CookieID = usr.CookieID
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

CREATE TABLE #Tempa_Favorites (
	RowNumber INT IDENTITY (1, 1) NOT NULL,
	ID int	
)

DECLARE @sql nvarchar(2000)
DECLARE @Top int

SET @Top = @Page*@PageSize
IF @PageSize > 0
   SET ROWCOUNT @Top
-- insert primary keys into temp table
SET @sql =	N'INSERT INTO #Tempa_Favorites ([ID]) SELECT '
SET @sql = @sql + ' [ID] FROM [dbo].[a_Favorites] WHERE UserID=' + Convert(nvarchar(50),@usrId) +  '  ORDER BY [' + @OrderBy + N'] ' + @OrderDirection
EXEC (@sql)
SET ROWCOUNT 0

SELECT @TotalRecords = COUNT(*) FROM [a_Favorites]

SELECT
	[dbo].[a_Favorites].[ID],
	[UserID],
	[DateAdded],
	[AlbumID],
	[FavoriteType],
	[FavoriteID]
FROM
	#Tempa_Favorites AS tblTemp JOIN [dbo].[a_Favorites] ON
	tblTemp.ID = [dbo].[a_Favorites].ID 	
WHERE (@PageSize = 0) OR (@PageSize > 0 AND (@Page - 1)*@PageSize < RowNumber AND RowNumber <= @Page*@PageSize)
ORDER BY RowNumber

DROP TABLE #Tempa_Favorites

